<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="120px">
      <el-form-item label="审核标准号" prop="criteriaNumber">
        <el-input
          v-model="queryParams.criteriaNumber"
          placeholder="请输入审核标准号"
          clearable
        />
      </el-form-item>
      <el-form-item label="审核标准名称" prop="criteriaName">
        <el-input
          v-model="queryParams.criteriaName"
          placeholder="请输入审核标准名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="审核项编号" prop="itemNumber">
        <el-input
          v-model="queryParams.itemNumber"
          placeholder="请输入审核项编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="审核项名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入审核项名称"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-perms="['qa:audit_criteria:add']"
          v-preventReClick
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="audit_criteriaList">
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="审核标准号" align="center" prop="criteriaNumber" />
      <el-table-column label="审核标准名称" align="center" prop="criteriaName" />
      <el-table-column label="审核项编号" align="center" prop="itemNumber" />
      <el-table-column label="审核项名称" align="center" prop="itemName" />
      <el-table-column label="评分标准" align="center" prop="scoreStandard" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <status-tag :status-enum="statusOptions" :status="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-perms="['qa:audit_criteria:edit']"
            v-preventReClick
          >修改
          </el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-perms="['qa:audit_criteria:remove']"
            v-preventReClick
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total>0"
      v-model:current-page="queryParams.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      style="float: right"
    />

    <!-- 添加或修改审核标准管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="审核标准号" prop="criteriaNumber">
          <el-input v-model="form.criteriaNumber" placeholder="请输入审核标准号" />
        </el-form-item>
        <el-form-item label="审核标准名称" prop="criteriaName">
          <el-input v-model="form.criteriaName" placeholder="请输入审核标准名称" />
        </el-form-item>
        <el-form-item label="审核项编号" prop="itemNumber">
          <el-input v-model="form.itemNumber" placeholder="请输入审核项编号" />
        </el-form-item>
        <el-form-item label="审核项名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入审核项名称" />
        </el-form-item>
        <el-form-item label="评分标准" prop="scoreStandard">
          <el-input v-model="form.scoreStandard" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="item in convertToOptions(statusOptions)" :key="item.value" size="large"
                      :value="item.value.toString()">{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import { AuditCriteriaService } from '@/api/qa/audit_criteria.js'
import StatusTag from '@/components/StatusTag.vue'
import { convertToOptions } from '@/utils/util.js'

export default {
  name: 'Audit_criteria',
  components: { StatusTag },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 审核标准管理表格数据
      audit_criteriaList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        criteriaNumber: null,
        criteriaName: null,
        itemNumber: null,
        itemName: null,
        scoreStandard: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        criteriaNumber: [
          { required: true, message: '审核标准号不能为空', trigger: 'blur' }
        ],
        criteriaName: [
          { required: true, message: '审核标准名称不能为空', trigger: 'blur' }
        ],
        itemNumber: [
          { required: true, message: '审核项编号不能为空', trigger: 'blur' }
        ],
        itemName: [
          { required: true, message: '审核项名称不能为空', trigger: 'blur' }
        ],
        scoreStandard: [
          { required: true, message: '评分标准不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger:'blur' }
        ]
      },
      statusOptions:{
        '0':{
          type:'success',
          txt:'已审核'
        },
        '1':{
          type:'primary',
          txt:'待审核'
        },
        '2':{
          type:'info',
          txt:'已废弃'
        }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    convertToOptions,
    /** 查询审核标准管理列表 */
    getList() {
      this.loading = true
      AuditCriteriaService.listAudit_criteria(this.queryParams).then(response => {
        this.audit_criteriaList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        criteriaNumber: null,
        criteriaName: null,
        itemNumber: null,
        itemName: null,
        scoreStandard: null,
        remark: null,
        createTime: null,
        updateTime: null,
        status:'1'
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加审核标准管理'
      this.reset()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      AuditCriteriaService.getAudit_criteria(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改审核标准管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            AuditCriteriaService.updateAudit_criteria(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            AuditCriteriaService.addAudit_criteria(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除审核标准管理编号为"' + ids + '"的数据项？').then(function() {
        return AuditCriteriaService.delAudit_criteria(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
